<?php
namespace Orm\ProductImg;

use Base\Exception\RmException;
class ProductImgClass extends \Base\BaseOrmObject{
    protected   $id,
				$product_id = 0, // 产品ID
				$url = '', // 图片地址
				$sort = 0, // 商品排序
				$is_del = 0, // 默认0  是否删除 1是 0否
				$insert_time = '', // 插入时间
				$update_time = ''; // 更新时间

    function __init($data)
    {
        parent::__init($data);
        $this->dformat($data);
    }

    private function dformat()
    {
    }
    public function loadById($id = 0)
    {
        $model = new \Orm\ProductImg\ProductImgModel();
        $data = $model->loadById($id);
        self::__init($data);
    }
    public function loadByIdWithDel($id = 0)
    {
        $model = new \Orm\ProductImg\ProductImgModel();
        $data = $model->loadByIdWithDel($id);
        self::__init($data);
    }

    public function insert()
    {
        $this->checkNecessaryParam();
        $data = array(
            'product_id' => $this->product_id,
			'url' => $this->url,
			'sort' => $this->sort,
        );

        $model = new \Orm\ProductImg\ProductImgModel();
        $model->insert($data);
        $this->id = $model->getLastId();
    }

    public function update()
    {
        $this->checkNecessaryParam();
        $data = array(
            'product_id' => $this->product_id,
			'url' => $this->url,
			'sort' => $this->sort,
			'is_del' => $this->is_del, // 重新关联是用到
        );

        $model = new \Orm\ProductImg\ProductImgModel();
        $model->update($data, "`id` = {$this->id}");
    }

    public function delete($id)
    {
        $data = array('is_del'=>1);
        $model = new \Orm\ProductImg\ProductImgModel();
        $model->update($data, "`id` = {$id}");
    }

    private function checkNecessaryParam()
    {
    }
}